package com.lvmama.rhino.web;

import com.alibaba.fastjson.JSONObject;
import com.lvmama.entity.system.Menu;
import com.lvmama.entity.system.SysUser;
import com.lvmama.rhino.base.BaseControllerSupport;
import com.lvmama.rhino.base.WebUtils;
import com.lvmama.rhino.service.MenuService;
import com.lvmama.rhino.utils.Constants;
import com.lvmama.rhino.web.sso.SystemSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.SavedRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController extends BaseControllerSupport {

    @Autowired
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }


    private MenuService menuService;


    @RequestMapping("/login")
    @ResponseBody
    public Object login(@RequestParam("username") String username,
                        @RequestParam("password") String password, ServletRequest request) {
        SavedRequest savedRequest = WebUtils.getSaveRequest(request);
        JSONObject result = new JSONObject();
        result.put("code", "1");
        result.put("url", savedRequest);
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            result.put("code", "-1");
            result.put("message", "用户名/密码不能为空！");
        } else {
            UsernamePasswordToken token = new UsernamePasswordToken(
                    username, password);
            try {
                SecurityUtils.getSubject().login(token);
            } catch (AuthenticationException e) {
                result.put("code", "-1");
                if (e.getMessage() != null
                        && StringUtils.startsWith(e.getMessage(), "msg:")) {
                    result.put("message",
                            StringUtils.replace(e.getMessage(), "msg:", ""));
                } else {
                    logger.error("登录失败！", e);
                    result.put("message", "登录失败，用户名或密码错误！");
                }
            }

        }
        return result;
    }


    @RequestMapping("index")
    public String index(ModelMap model, String mainFrame, HttpServletRequest request) {
        SysUser currentUser = SystemSession.getUser();
        List<Menu> menus = currentUser.getRoles().get(0).getMenuList();
        List<Menu> sysMenus = new ArrayList<>();
        for (Menu menu : menus) {
            if (currentUser.getRemarks().equals(menu.getSysCode())) {
                sysMenus.add(menu);
            }
        }
        //设置用户登录信息
        UsernamePasswordToken token = new UsernamePasswordToken(
                currentUser.getUsername(), Constants.PASSWORD);
        SecurityUtils.getSubject().login(token);
        if (currentUser.getIsReset().equals("0")) {
            mainFrame = "/rhino/user/editInfo";
        }
        model.addAttribute("menus", sysMenus);
        model.addAttribute("currentUser", currentUser);
        model.addAttribute("mainFrame", mainFrame);
        return "index";
    }

    @RequestMapping("logout")
    public String logout() {
        SecurityUtils.getSubject().logout();

        return "redirect:/user/index";
    }

    @RequestMapping("indexRefresh")
    public String indexRefresh(Model model) {
        return "indexRefresh";
    }
}
